<template>
	<div class="user-list">
		<el-table ref="multipleTable" style="width: 100%" stripe :data="tableData" row-key="id" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" reserve-selection></el-table-column>
			<el-table-column fixed prop="userName" label="用户" width="200" show-overflow-tooltip>
				<template slot-scope="scope">
					{{ scope.row | showUserInfo }}
				</template>
			</el-table-column>

			<el-table-column prop="deptFullName" label="部门" show-overflow-tooltip>
				<template slot-scope="scope">
					{{ scope.row.deptFullName | deptFormat }}
				</template>
			</el-table-column>
			<el-table-column prop="roleString" label="角色" show-overflow-tooltip>
				<template slot-scope="{ row }">
					{{ row.menuRoleList | roleFilter }}
				</template>
			</el-table-column>
			<el-table-column prop="del" label="状态" width="80px">
				<template slot-scope="{ row }">
					<span class="color-success" v-show="row.del == 1">激活</span>
					<span class="color-danger" v-show="row.del == 2">封禁</span>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" width="180px"></el-table-column>
			<el-table-column label="操作" width="120px">
				<template slot-scope="{ row }">
					<el-dropdown split-button type="primary" size="mini">
						<span type="primary" @click="handleEdit(row)" :disabled="!$button('744')">编辑</span>
						<el-dropdown-menu slot="dropdown">
							<el-dropdown-item @click.native="handleDelete(row)" :disabled="!$button('745')" v-if="row.id != 1">
								<span>删除</span>
							</el-dropdown-item>
							<el-dropdown-item @click.native="handleBanOrRecover([row], 'ban')" :disabled="!$button('746')" v-show="row.del != '2'" v-if="row.id != 1">
								<span>封禁</span>
							</el-dropdown-item>
							<el-dropdown-item @click.native="handleBanOrRecover([row], 'recover')" :disabled="!$button('746')" v-show="row.del == '2'" v-if="row.id != 1">
								<span>激活</span>
							</el-dropdown-item>
							<el-dropdown-item @click.native="handleResetPassword(row)" :disabled="!$button('747')"><span>重置密码</span></el-dropdown-item>
							<el-dropdown-item @click.native="mailboxAdministration(row)" v-if="judgeMailRole(row)"><span>设置信箱管理员</span></el-dropdown-item>
							<!-- <el-dropdown-item @click.native="handleColumnPermission(row)" v-if="row.id != 1">
								<span>栏目权限管理</span>
							</el-dropdown-item> -->
						</el-dropdown-menu>
					</el-dropdown>
				</template>
			</el-table-column>
		</el-table>
		<Pagination
			style="margin-top: 20px"
			v-show="parseInt(pageObj.count) > 0"
			:total="parseInt(pageObj.count)"
			@change="getList"
			:page.sync="listQuery.page"
			:limit.sync="listQuery.limit"
		/>
	</div>
</template>
<script>
//封禁|激活方法
import banned from '../mixin/banned';
export default {
	mixins: [banned],
	data() {
		return {
			listQuery: {
				page: 1,
				limit: 10,
				userName: '',
				education: '',
				account: '',
				del: '',
				isChildUser: '1'
			},
			tableData: [],
			pageObj: {},
			multipleSelection: []
		};
	},
	mounted() {
		this.$bus.$on('refreshUserList', (v) => {
			if (v) {
				Object.assign(this.listQuery, v);
			}
			this.getList();
		});
		this.getList();
	},
	beforeDestroy() {
		this.$bus.$off('refreshUserList');
	},
	methods: {
		async getList(flag) {
			if (flag) {
				let { userType, deptId, deptFullData } = this.$store.getters;
				if (userType == 1) {
					this.listQuery.deptId = '';
				} else {
					if (deptId) {
						this.listQuery.deptId = deptId;
					} else if (deptFullData.hasOwnProperty('childList') && deptFullData.childList.length) {
						let schoolDept = deptFullData.childList[0].id;
						this.listQuery.deptId = schoolDept;
					}
				}
			}
			let res = await this.$store.dispatch('queryTbUserList', this.listQuery);
			this.tableData = res.data;
			this.pageObj = res;
		},
		handleEdit(row) {
			this.$bus.$emit('changeUserTemplate', 'Add', row.id);
		},
		//重置密码确认
		handleResetPassword(row) {
			this.deleteTips(
				() => {
					this.resetPassword(row);
				},
				'是否重置密码?',
				'重置'
			);
		},
		//重置密码
		async resetPassword(row) {
			let password = await this.$store.dispatch('getDictByCode', 'USER_PASSWORD');
			let obj = { id: row.id, password };
			await this.$store.dispatch('resetPasswordTbUser', [obj]);
			this.successMsg('密码已重置');
		},
		//删除确认
		handleDelete(row) {
			this.deleteTips(() => {
				this.delete(row);
			}, '删除人员将严重影响平台功能, 是否删除?');
		},
		//删除
		async delete(row) {
			let obj = {
				id: row.id,
				list: row
			};
			await this.$store.dispatch('deleteTbUser', [obj]);
			this.getList();
		},
		handleSelectionChange(val) {
			this.multipleSelection = val;
			this.$store.commit('SET_USER', val);
		},
		//栏目权限
		handleColumnPermission(row) {
			this.$bus.$emit('showDialogColumnPermission', row);
		},
		//设为信箱管理员
		mailboxAdministration(row) {
			this.$bus.$emit('showDialogmailboxAdministration', row);
		},

		//判断是否信箱角色
		judgeMailRole(data) {
			let roles = data.menuRoleList || [];
			return roles.some((n) => n.name === '信箱管理');
		}
	},
	filters: {
		roleFilter(list) {
			return list.length > 0 ? list.map((v) => v.name).toString() : '--';
		}
	}
};
</script>
<style scoped lang="scss"></style>
